//
//  JKViewProductDetailStoreDetail.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/29.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKViewProductDetailStoreDetail.h"

@implementation JKViewProductDetailStoreDetail
{
    UILabel     *_titleLabel;
    UILabel     *_descLabel;
    UIView      *_pictureView;
}
- (instancetype)init {
    
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 12;
        self.layer.masksToBounds = true;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _titleLabel = [UILabel new];
    _titleLabel.numberOfLines = 0;
    _titleLabel.text = @"商家详情";
    _titleLabel.font = FONT(15);
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.textColor = [UIColor blackColor];
    [self addSubview:_titleLabel];
    
    _descLabel = [UILabel new];
    _descLabel.numberOfLines = 0;
    _descLabel.text = @"商家详情";
    _descLabel.font = FONT(15);
    _descLabel.textAlignment = NSTextAlignmentLeft;
    _descLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:_descLabel];
    
    _pictureView = [UIView new];
    [self addSubview:_pictureView];
    
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@10);
        make.right.equalTo(@-10);
    }];
    
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_titleLabel);
        make.top.equalTo(_titleLabel.mas_bottom).offset(10);
    }];
   
    [_pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(_descLabel.mas_bottom).offset(5);
        make.right.equalTo(@-10);
        
        make.bottom.equalTo(@-0);
    }];
    
    [self createPictures];
}

- (void)createPictures {
    
    [_pictureView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSArray *pictures = @[@"商城详情-死图2", @"商城详情-死图3", @"商城详情-死图4", @"商城详情-死图1", @"商城详情-死图2", @"商城详情-死图3"];
    if (_dic) {
        pictures = [_dic[@"img"] componentsSeparatedByString:@","];
    }
    UIView *last = nil;
    for (int i = 0; i < pictures.count; i ++) {
        
        UIImageView *view = [UIImageView new];
        if (_dic) {

            [view setImageWithURL:[NSURL URLWithString:BOYE_PICTURE_VIEW_URL(pictures[i])] placeholderImage:DEFAULT_IMAGE completed:nil];
        } else {
            view.image = [UIImage imageNamed:pictures[i]];
        }
        [_pictureView addSubview:view];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (last) {
                // 每一行的第一个，last是上一行的右侧那个
                if (i % 3 == 0) {
                    make.left.equalTo(@0);
                    make.width.equalTo(last);
                    make.top.equalTo(last.mas_bottom).offset(10);
                    make.height.equalTo(last);
                }
                // last是本行的前一个
                else {
                    make.left.equalTo(last.mas_right).offset(10);
                    make.width.equalTo(last);
                    make.top.equalTo(last);
                    make.height.equalTo(last);
                }
            } else {
                make.left.equalTo(@0);
                make.width.equalTo(@((SCREEN_WIDTH - 20 * 2 - 10 * 2) / 3));
                make.top.equalTo(@10);
                make.height.equalTo(view.mas_width);
            }
            
            // 设置cell高度
            if (i == pictures.count - 1) {
                make.bottom.equalTo(@-10);
            }
        }];
        
        last = view;
    }
}

- (void)setDic:(NSDictionary *)dic {
    
    _dic = dic;

    if ([_dic.allKeys containsObject:@"null"]) {
        [self removeFromSuperview];
    }
    else {
        _titleLabel.text = dic[@"title"];
        _descLabel.text = dic[@"detail"];
        [self createPictures];
    }
}
@end
